package scau.redi.social_platform.common.util;

import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.util.List;

/**
 * 分页响应数据
 * @author zhangshaolong
 * @date 2021/6/6
 */
@Data
public class ResponsePageUtil<T> {

    private int pageNum;

    private int pageSize;

    private int totalPage;

    private long totalItemNum;

    private List<T> data;

    public ResponsePageUtil() {

    }

    public ResponsePageUtil(int pageNum, int pageSize, int totalPage, int totalItemNum, List<T> data) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.totalItemNum = totalItemNum;
        this.totalPage = totalPage;
        this.data = data;
    }

    /**
     * 将pageHelper分页后的list转化为ResponsePageUtil分页对象
     */
    public static <T> ResponsePageUtil<T> restPage(List<T> list) {
        ResponsePageUtil<T> result = new ResponsePageUtil<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);

        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalItemNum(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }


}
